package com.example.model.controller;

import com.example.model.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet implementation class loginServlet
 */
public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userName, password;
        UserDao dao = new UserDao();
        int result = 0;
        //1.调用请求对象对请求体中采用utf-8字符集进行重新编辑
        request.setCharacterEncoding("utf-8");
        //2.调用请求对象获取请求体参数信息
        userName = request.getParameter("userName");
        password = request.getParameter("password");

        //3.调用Dao将查询验证信息推送到数据库服务器
        result = dao.login(userName, password);
        //4.调用相应对象，根据验证结果将不同的资源的文件地址写入到相应头，交给浏览器
        if (result == 1) {//结果等于1用户存在
            response.sendRedirect("/myWeb/index.html");
        } else {
            response.sendRedirect("/myWeb/login_error.html");
        }
    }

}
